package com.o2o.app.service;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.o2o.app.R;
import com.o2o.app.bean.ServiceEditBean;
import com.o2o.app.userCenter.Demo1;
import com.o2o.app.userCenter.MineDiscloseActivity;
import com.o2o.app.userCenter.MineHelpActivity;
import com.o2o.app.utils.AlertTools;

public class CommunityServiceActivity extends Activity {
	/**
	 * 返回按钮
	 */
	private Button btn_back;

	/**
	 * 右上角按钮
	 */
	private Button btn_publish;

	/**
	 * 判断是显示编辑还是完成 false = 编辑，true = 完成
	 */
	private Boolean b_youshang = false;

	/**
	 * 编辑开关按钮
	 */
	private CheckBox cb_edit04, cb_edit05, cb_edit06;

	/**
	 * 服务类列表
	 */
	private ArrayList<ServiceEditBean> serviceList = new ArrayList<ServiceEditBean>();

	/**
	 * 服务功能键1
	 */
	private RelativeLayout rlt_title1;

	/**
	 * 服务功能键2
	 */
	private RelativeLayout rlt_title2;

	/**
	 * 服务功能键3
	 */
	private RelativeLayout rlt_title3;

	/**
	 * 功能键图片1
	 */
	private ImageView iv_01;

	/**
	 * 功能键图片2
	 */
	private ImageView iv_02;

	/**
	 * 功能键图片3
	 */
	private ImageView iv_03;

	/**
	 * 功能键文字1
	 */
	private TextView tv_01;

	/**
	 * 功能键文字2
	 */
	private TextView tv_02;

	/**
	 * 功能键文字3
	 */
	private TextView tv_03;

	/**
	 * 扫一扫按钮
	 */
	private RelativeLayout rlt_08;

	/**
	 * 驿站女孩按钮
	 */
	private RelativeLayout rlt_07;

	/**
	 * 生活黄页
	 */
	private RelativeLayout rlt_06;

	/**
	 * 北青社区报
	 */
	private RelativeLayout rlt_15;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.community_service);

		initViews();
	}

	private void initViews() {
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new ClickEvent());

		btn_publish = (Button) findViewById(R.id.btn_publish);
		btn_publish.setOnClickListener(new ClickEvent());

		cb_edit04 = (CheckBox) findViewById(R.id.cb_edit04);
		cb_edit05 = (CheckBox) findViewById(R.id.cb_edit05);
		cb_edit06 = (CheckBox) findViewById(R.id.cb_edit06);

		rlt_title1 = (RelativeLayout) findViewById(R.id.rlt_title1);
		rlt_title1.setOnClickListener(new ClickEvent());

		rlt_title2 = (RelativeLayout) findViewById(R.id.rlt_title2);
		rlt_title2.setOnClickListener(new ClickEvent());

		rlt_title3 = (RelativeLayout) findViewById(R.id.rlt_title3);
		rlt_title3.setOnClickListener(new ClickEvent());

		iv_01 = (ImageView) findViewById(R.id.iv_01);
		iv_02 = (ImageView) findViewById(R.id.iv_02);
		iv_03 = (ImageView) findViewById(R.id.iv_03);

		tv_01 = (TextView) findViewById(R.id.tv_01);
		tv_02 = (TextView) findViewById(R.id.tv_02);
		tv_03 = (TextView) findViewById(R.id.tv_03);

		// 初始化服务，将所有服务放到列表 物业通知
		addServiceList(R.drawable.wuyetongzhi, R.string.service_text_info_04,
				ServiceGirlActivity.SGA, cb_edit04);

		// 驿站通告
		addServiceList(R.drawable.yizhangonggao, R.string.service_text_info_05,
				MineDiscloseActivity.MDA, cb_edit05);

		// 邻里求助
		addServiceList(R.drawable.shequhuodong, R.string.service_text_info_06,
				MineHelpActivity.MHA, cb_edit06);

		rlt_08 = (RelativeLayout) findViewById(R.id.rlt_08);
		rlt_08.setOnClickListener(new ClickEvent());

		rlt_07 = (RelativeLayout) findViewById(R.id.rlt_07);
		rlt_07.setOnClickListener(new ClickEvent());

		rlt_06 = (RelativeLayout) findViewById(R.id.rlt_06);
		rlt_06.setOnClickListener(new ClickEvent());

		rlt_15 = (RelativeLayout) findViewById(R.id.rlt_15);
		rlt_15.setOnClickListener(new ClickEvent());
	}

	private void addServiceList(int drawable, int text, Activity activty,
			CheckBox cb) {

		ServiceEditBean bean = new ServiceEditBean();
		bean.setText(getResources().getString(text));
		bean.setDrawpic(getResources().getDrawable(drawable));
		bean.setActivity(activty);
		bean.setCb(cb);

		serviceList.add(bean);
	}

	/**
	 * 遍历list
	 */
	@SuppressLint("NewApi")
	private void checkList() {
		int index = 0; // 从1开始放到响应的服务框中
		for (int i = 0; i < serviceList.size(); i++) {
			if (serviceList.get(i).getCb().isChecked()) { // 点击为真
				index++;
				switch (index) {
				case 1: {
					iv_01.setBackground(serviceList.get(i).getDrawpic());
					tv_01.setText(serviceList.get(i).getText());
				}
					break;
				case 2: {
					iv_02.setBackground(serviceList.get(i).getDrawpic());
					tv_02.setText(serviceList.get(i).getText());
				}
					break;
				case 3: {
					iv_03.setBackground(serviceList.get(i).getDrawpic());
					tv_03.setText(serviceList.get(i).getText());
				}
					break;
				default:
					break;
				}
			}
		}

		if (index < 3) {
			AlertTools.showConfirmAlert(CommunityServiceActivity.this,
					"请选择3个服务功能", "确定");
		}
	}

	/**
	 * 点击事件
	 * 
	 * @author Administrator
	 * 
	 */
	class ClickEvent implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {

			case R.id.btn_back: { // 返回
				finish();
			}
				break;

			case R.id.btn_publish: { // 右上角按钮
				b_youshang = !b_youshang;
				if (b_youshang) { // 点击编辑

					btn_publish.setText("完成");

					// 显示编辑框
					showCheckBox();
				} else { // 点击完成
					btn_publish.setText("编辑");
					checkList();
					// 隐藏编辑框
					hideCheckBox();
				}
			}
				break;

			case R.id.rlt_title1: {

			}
				break;

			case R.id.rlt_title2: {

			}
				break;

			case R.id.rlt_title3: {

			}
				break;
			case R.id.rlt_08: { // 扫一扫
				Intent intent = new Intent(CommunityServiceActivity.this,
						Demo1.class);

				startActivity(intent);
			}
				break;

			case R.id.rlt_07: { // 驿站女孩
				Intent intent = new Intent(CommunityServiceActivity.this,
						ServiceGirlActivity.class);
				// 从服务中进入，title有首页按钮
				intent.putExtra("forhome", "forhome");
				startActivity(intent);

			}
				break;

			case R.id.rlt_06: { // 生活黄页

			}
				break;

			case R.id.rlt_15: { // 北青社区报
				Intent intent = new Intent(CommunityServiceActivity.this,
						BQNewspaperActivity.class);
				startActivity(intent);

			}
				break;
			default:
				break;
			}
		}
	}

	/**
	 * 显示编辑框
	 */
	private void showCheckBox() {
		cb_edit04.setVisibility(View.VISIBLE);
		cb_edit05.setVisibility(View.VISIBLE);
		cb_edit06.setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏编辑框
	 */
	private void hideCheckBox() {
		cb_edit04.setVisibility(View.INVISIBLE);
		cb_edit05.setVisibility(View.INVISIBLE);
		cb_edit06.setVisibility(View.INVISIBLE);
	}

}
